<?php



class ShoppingCart

{

	private $items = array();
	private $itemsQuantity = array();
	private $total = 0;
	
	private $items2 = array();
	private $itemsQuantity2 = array();
	private $total2 = 0;

	public function __construct()

	{

	}

	public function __construct2()

	{

	}

	public static function getInstance()

	{

		if(!isset($_SESSION['shopping_cart']))

		{

			$_SESSION['shopping_cart'] = serialize(new ShoppingCart());

		}

		

		return unserialize($_SESSION['shopping_cart']);

	}

	public static function getInstance2()

	{

		if(!isset($_SESSION['shopping_cart2']))

		{

			$_SESSION['shopping_cart2'] = serialize(new ShoppingCart());

		}

		

		return unserialize($_SESSION['shopping_cart2']);

	}

	public static function updateInstance($new)

	{

		$_SESSION['shopping_cart'] = serialize($new);

	}

	public static function updateInstance2($new)

	{

		$_SESSION['shopping_cart2'] = serialize($new);

	}

	public function addItem($item)

	{

		if (get_class($item) != 'CartItem')

		{

			return;

		}

		

		$id = $item->getId();

		

		if (isset($this->itemsQuantity[$id]) && $this->itemsQuantity[$id] > 0)

		{

			$this->itemsQuantity[$id] += 1;

			$this->items[$id]->setQuantity($this->itemsQuantity[$id]);

			

			$this->updateTotal();

			

			return;

		}

		

		$this->itemsQuantity[$id] = 1;

		$this->items[$id] = $item;

		

		$this->updateTotal();

		

	}

	public function addItem2($item)

	{

		if (get_class($item) != 'CartItem')

		{

			return;

		}

		

		$id = $item->getId();

		

		if (isset($this->itemsQuantity[$id]) && $this->itemsQuantity[$id] > 0)

		{

			$this->itemsQuantity[$id] += 1;

			$this->items[$id]->setQuantity($this->itemsQuantity[$id]);

			

			$this->updateTotal();

			

			return;

		}

		

		$this->itemsQuantity[$id] = 1;

		$this->items[$id] = $item;

		

		$this->updateTotal();

		

	}

	public function updateQuantity($id, $qty)

	{

		$qty = (int) $qty;

		

		$index = 0;

		

		foreach($this->items as &$item)

		{

			if($item->getId() == $id)

			{

				// delete

				

				if ($qty <= 0)

				{

					unset($this->items[$id]);

					unset($this->itemsQuantity[$item->getId()]);

				}

				else

				{

					$item->setQuantity($qty);

				}

				break;

			}

			++$index;

		}

		

		$this->updateTotal();



	}

		public function updateQuantity2($id, $qty)

	{

		$qty = (int) $qty;

		

		$index = 0;

		

		foreach($this->items as &$item)

		{

			if($item2->getId() == $id)

			{

				// delete

				

				if ($qty <= 0)

				{

					unset($this->items[$id]);

					unset($this->itemsQuantity[$item->getId()]);

				}

				else

				{

					$item->setQuantity($qty);

				}

				break;

			}

			++$index;

		}

		

		$this->updateTotal();



	}

	public function deleteItem($id)

	{

		$this->updateQuantity($id, -1);

	}

	public function deleteItem2($id)

	{

		$this->updateQuantity2($id, -1);

	}

	public function emptyCart()

	{

		unset($this->items);

		unset($this->itemsQuantity);

		

		$this->updateTotal();

	}

	

	public function getItems()

	{

		return $this->items;

	}

	

	public function countItems()

	{

		return count($this->items);

	}

	public function getItems2()

	{

		return $this->items;

	}

	

	public function countItems2()

	{

		return count($this->items);

	}

	

	public function getTotal()

	{

		return $this->total;

	}

	

	public function updateTotal()

	{

		$this->total = 0;

		if (sizeof($this->items) > 0)

		{

			foreach ($this->items as $item)

			{

				$this->total += ($item->getPrice() * $item->getQuantity());

			}

		}

	}

	

}



?>